package at.fhooe.mc.msports;

import java.util.Vector;

import net.rim.device.api.i18n.DateFormat;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;
import at.fhooe.mc.msports.ui.ColorableLabel;

public final class StatisticsScreen extends MainScreen
{
	private ColorableLabel lblHeader;
	private ColorableLabel lblWeeksAverageDesc;
	private ColorableLabel lblWeeksAverage;
	private ColorableLabel lblMaxStepsDesc;
	private ColorableLabel lblMinStepsDesc;
	private ColorableLabel lblMaxSteps;
	private ColorableLabel lblMinSteps;
	
    public StatisticsScreen()
    {        
    	initialize();
    }
    
    private void initialize(){
    	VerticalFieldManager mgr = (VerticalFieldManager)getMainManager();
    	mgr.setBackground(BackgroundFactory.createSolidBackground(Color.BLACK));
    	
    	lblHeader = new ColorableLabel("Statstics", 
    			Color.LIGHTBLUE, USE_ALL_WIDTH|DrawStyle.HCENTER, 
    			Font.getDefault().derive(Font.PLAIN,40));
    	lblHeader.setMargin(10,0,0,0);
    	add(lblHeader);
    	
    	//Show the last (max. 7) sessions
    	Vector session = SessionManager.claimLastWeeksSessions();
    	if(session != null){
    		
    		ColorableLabel lblValue;
    		ColorableLabel lblDescription;
    		HorizontalFieldManager hfm;
    		
    		Session s;
    		String descriptiveText;
    		String value;
    		
    		//show last (max. 7) sessions
    		int size = session.size() > 7 ? 7 : session.size();
    		
    		for(int i = size-1;i>=0;i--){
    			s = ((Session)session.elementAt(i));
    			
	    		    StringBuffer sb = new StringBuffer();
	
	    		    DateFormat.getInstance(DateFormat.DATE_LONG).formatLocal
	    		    	(sb,s.getTimeStamp());
	    		    
	    		    descriptiveText = sb.toString();
	    		    
	    		    value = String.valueOf(s.getSteps());
	    		    
	    		    if(s.getSteps()>=1000){
	    		    	String str = String.valueOf(s.getSteps()%1000);
	    		    	
	    		    	if(s.getSteps()%1000 <= 99)
	    		    		str = "0" + String.valueOf(s.getSteps()%1000);
	    		    	
	    		    	if(s.getSteps()%1000 == 0)
	    		    		str = "000";
	    		    	
	    		    	value = String.valueOf(s.getSteps()/1000 + "." + str);
	    		    }
	    			
	    			//Create descriptive label
	    			lblDescription = new ColorableLabel(descriptiveText, 
	    	    			Color.WHITE, 0, 
	    	    			Font.getDefault().derive(Font.PLAIN,25));
	    			
	    			//Create value label
	    			lblValue = new ColorableLabel(value, 
	    	    			Color.YELLOW,USE_ALL_WIDTH|DrawStyle.RIGHT, 
	    	    			Font.getDefault().derive(Font.PLAIN,25));
	    			
	    			hfm = new HorizontalFieldManager();
	    			hfm.add(lblDescription);
	    			hfm.add(lblValue);
	    			lblValue.setMargin(0,40,0,0);
	    			if(i == size-1)
	    				hfm.setMargin(20,0,0,20);
	    			else
	    				hfm.setMargin(10,0,0,20);
	    			add(hfm);
    		}
    	}else{
    		ColorableLabel lbl = new ColorableLabel("Currently no sessions available", 
	    			Color.WHITE, USE_ALL_WIDTH|DrawStyle.HCENTER, 
	    			Font.getDefault().derive(Font.PLAIN,25));
    		
    		lbl.setMargin(60,0,0,0);
    		add(lbl);
    	}
    	
    	//Average step count
    	HorizontalFieldManager average = new HorizontalFieldManager();
    	
    	lblWeeksAverageDesc = new ColorableLabel("Last weeks avg.", 
    			Color.LIGHTBLUE, 0, 
    			Font.getDefault().derive(Font.PLAIN,25));
    	
    	lblWeeksAverageDesc.setMargin(40,0,0,20);
    	average.add(lblWeeksAverageDesc);
    	
    	lblWeeksAverage = new ColorableLabel("", 
    			Color.WHITE, USE_ALL_WIDTH|DrawStyle.RIGHT, 
    			Font.getDefault().derive(Font.PLAIN,25));
    	lblWeeksAverage.setMargin(40,40,0,0);
    	average.add(lblWeeksAverage);
    	add(average);
    	
    	//Max step count
    	HorizontalFieldManager max = new HorizontalFieldManager();
    	
    	lblMaxStepsDesc = new ColorableLabel("Max. steps", 
    			Color.LIGHTBLUE, 0, 
    			Font.getDefault().derive(Font.PLAIN,25));
    	
    	lblMaxStepsDesc.setMargin(10,0,0,20);
    	max.add(lblMaxStepsDesc);
    	
    	lblMaxSteps = new ColorableLabel("", 
    			Color.WHITE, USE_ALL_WIDTH|DrawStyle.RIGHT, 
    			Font.getDefault().derive(Font.PLAIN,25));
    	lblMaxSteps.setMargin(10,40,0,0);
    	max.add(lblMaxSteps);
    	add(max);
    	
    	//Min step count
    	HorizontalFieldManager min = new HorizontalFieldManager();
    	
    	lblMinStepsDesc = new ColorableLabel("Min. steps", 
    			Color.LIGHTBLUE, 0, 
    			Font.getDefault().derive(Font.PLAIN,25));
    	
    	lblMinStepsDesc.setMargin(10,0,0,20);
    	min.add(lblMinStepsDesc);
    	
    	lblMinSteps = new ColorableLabel("", 
    			Color.WHITE, USE_ALL_WIDTH|DrawStyle.RIGHT, 
    			Font.getDefault().derive(Font.PLAIN,25));
    	lblMinSteps.setMargin(10,40,0,0);
    	min.add(lblMinSteps);
    	add(min);
    	
    	if(session != null){
    		
    		//Average
    		int size = session.size() > 7 ? 7 : session.size();
    		int avg = 0;
    		for(int i = 0; i < size; i++){
    			avg += ((Session)session.elementAt(i)).getSteps();
    		}
    		
    		avg /= size;
    		
    		String value = String.valueOf(avg);
		    
		    if(avg>=1000){
		    	String str = String.valueOf(avg%1000);
		    	
		    	if(avg%1000 <= 99)
		    		str = "0" + String.valueOf(avg%1000);
		    	
		    	if(avg%1000 == 0)
		    		str = "000";
		    	
		    	value = String.valueOf(avg/1000 + "." + str);
		    }
    		lblWeeksAverage.setText(value);
    		
    		//Max
    		value = String.valueOf(SessionManager.getMaximalStepCount().getSteps());
		    avg = SessionManager.getMaximalStepCount().getSteps();
		    if(avg>=1000){
		    	String str = String.valueOf(avg%1000);
		    	
		    	if(avg%1000 <= 99)
		    		str = "0" + String.valueOf(avg%1000);
		    	
		    	if(avg%1000 == 0)
		    		str = "000";
		    	
		    	value = String.valueOf(avg/1000 + "." + str);
		    }
    		lblMaxSteps.setText(value);
    		
    		//Min
    		value = String.valueOf(SessionManager.getMaximalStepCount().getSteps());
		    avg = SessionManager.getMinimalStepCount().getSteps();
		    if(avg>=1000){
		    	String str = String.valueOf(avg%1000);
		    	
		    	if(avg%1000 <= 99)
		    		str = "0" + String.valueOf(avg%1000);
		    	
		    	if(avg%1000 == 0)
		    		str = "000";
		    	
		    	value = String.valueOf(avg/1000 + "." + str);
		    }
		    
		    if(avg == 0)
		    	value = String.valueOf(0);
		    
    		lblMinSteps.setText(value);
    	}else{
    		lblWeeksAverage.setText("-");
    		lblMinSteps.setText("-");
    		lblMaxSteps.setText("-");
    	}
    }
}
